$(function () {
    var guessInt = $('#guessInt'); // 输入框
    var guessBtn = $('#guessBtn'); //猜一猜按钮
    var judgeText = $('#judge-text'); // 提示
    var degree = $('#degreeNum'); //次数
    var handIndex = $('.handIndex');
    var answer; //答案
    var shadeBox = $('#shadeBox');
    var handImg = $('#handImg');
    var popAnswer = $('#pop-answer');

    var showHand = function () {
        var randowNum = parseInt(Math.random() * 10);
        answer = randowNum;
        handIndex.append('<img data-number="' + randowNum + '" src="img/' + randowNum + '.png" alt="">')
    }
    
    showHand();
    guessBtn.on('click', function () {
        var guessVal = guessInt.val(); //输入框 值
        if (!guessVal || guessVal.length > 1) {
            judgeText.html('输入错误 请重新输入');
        } else {
            judgeText.html('');
            var degreeNum = parseInt(degree.text());
//            guessInt.val('');
            switch (degreeNum) {
                case 3:
                    $('.mask-one').fadeOut('slow');
                    break;
                case 2:
                    $('.mask-two').fadeOut('slow');
                    break;
                case 1:
                    $('.mask-three').fadeOut('slow');
                    break;
            }
            if (degreeNum <= 1) {
                degree.text(0);
                $('#seeBtn').attr('src','img/seebtn.png');                
                if(parseInt(guessVal) == answer){
                    console.log('恭喜你');
                    $('#correctBox').fadeIn('slow');
                }else{
                    shadeBox.fadeIn('slow');
                    handImg.attr('src','img/'+ answer +'.png');
                    popAnswer.text(answer);
                    console.log('不好意思回答错误');
                }
                return false;
            } else {
                degreeNum--;
                degree.text(degreeNum);
            }
        }
    });


    $('#gueBtn').on('click',function(){
        shadeBox.fadeOut('slow');
        reduction();
    });
    
    $('#guessClose').on('click',function(){
       $('#correctBox').fadeOut('slow');
       reduction();
    });
    
    $('#convertClose').on('click',function(){
        $('.convert').hide();
    });
    
    $('#luckDraw').on('click',function(){
        $('.convert').fadeIn('slow');
        $('#correctBox').fadeOut('slow');
        reduction();
    });
    
    $('#recur').on('click',function(){
        $('#correctBox').fadeOut('slow');
        reduction();
    });
    
    var reduction = function(){
        handIndex.empty();
        shadeBox.fadeOut('slow');
        $('.mask > div').show();
        degree.text(3);
        showHand();
        $('#seeBtn').attr('src','img/button.png');
    }





});
